package com.ding.controller;

import com.ding.pojo.Books;
import com.ding.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import java.awt.print.Book;
import java.util.List;

/**
 * BookController
 *
 * @author DYS
 * @date 2021/7/6
 * @time 11:29
 * @Description
 */
@Controller
@RequestMapping("/book")
public class BookController {

	//controller 调用service
	@Autowired
	@Qualifier("bookService")
	private BookService bookService;

	//查询全部的书籍
	@RequestMapping("/allbook")
	public String showAllBooks(Model model) {
		List<Books> books = bookService.selectAllBooks();
		model.addAttribute("books", books);
		return "allBook";
	}

	//跳转到增加书籍页面
	@RequestMapping("/toaddbook")
	public String toAddPage() {
		return "addBook";
	}

	//增加书籍
	@RequestMapping("/addbook")
	public String addOneBook(Books book) {
		int row = bookService.insertOneBook(book);
		if (row > 0) {
			System.out.println("增加成功");
		} else {
			System.out.println("增加失败");
		}
		return "redirect:/book/allbook";
	}

	//跳转到修改书籍页面
	@RequestMapping("/toupdatepage")
	public String toupdatepage(HttpServletRequest req, @RequestParam("bookID") int bookID) {
		Books books = bookService.selectOneBook(bookID);
		req.setAttribute("books", books);
		return "updateBook";
	}

	//修改书籍
	@RequestMapping("/updatebook")
	public String updateOneBook(Books book) {
		int row = bookService.updateOneBook(book);
		if (row > 0) {
			System.out.println("修改成功");
		} else {
			System.out.println("修改失败");
		}
		return "redirect:/book/allbook";
	}

	//删除书籍
	@RequestMapping("/deletebook")
	public String deleteOneBook(@RequestParam("bookID") int bookID){
		int row = bookService.deleteOneBook(bookID);
		if (row > 0) {
			System.out.println("删除成功");
		} else {
			System.out.println("删除失败");
		}
		return "redirect:/book/allbook";
	}

	//模糊查询
	@RequestMapping("/onebook")
	public String selectOneBook(HttpServletRequest req,@RequestParam("bookName") String bookName){
		List<Books> books = bookService.selectLimitBook(bookName);
		req.setAttribute("books",books);
		return "oneBook";
	}
}
